<?php
/**
 * @file index.php 把输入的推广地址写入SQLite数据库，并随机生成推广table
 * @package 
 * @subpackage 
 * @version $id: 0.1 UTF-8 2009-3-14 上午12:45:55 $
 * @author LD King (mifunny.info)
 * @copyright Copyright (c) 2007 - 2009 MiFunny China Inc. (http://mifunny.info)
 */
define('DB_FILE', '7tao.db');
$submit = $_POST['submit'];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="zh-CN">
<head profile="http://gmpg.org/xfn/11">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>淘客地址提交 | 水草树</title>
<meta name="robots" content="nofollow" />
<link rel="stylesheet" href="style.css" type="text/css" media="screen" />
</head>
<body>
<div id="wrapper">
	<div id="header">
		<p><a href="cut.php">数据切割</a> | <a href="submit.php">提交淘客数据库</a> | <a href="obtain.php">随机取出淘客代码</a></p>
	</div>
<?php
if(isset($submit) and !empty($submit)) {
	$i_url = $_POST['url'];
	$i_title = $_POST['title'];
	$i_table = $_POST['table'];
	$i_table = 	str_replace ( "\'", "'", $i_table );
	$i_table = str_replace ( '\"', '"', $i_table );

	// 连接 SQLite 
	try {
		$dbh = new PDO("sqlite:".DB_FILE, null, null, array(PDO::ATTR_PERSISTENT => true));
	}catch (PDOException $ex){
		die("[SQLite Connect Error]: ".$ex->getMessage().'<br />\n');
	}
	$dbh->query('PRAGMA encoding = "UTF-8"');
	$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	
	$matches = array(); $taoke_info = array();
	$pattern = '/<table.*?<a.*?href="(.+?)".+?<img.+?src="(.+?)".+?<a.+?>(.+?)<\/a>.+?<span.+?>(.+?)<\/span>.+?<\/table>/s';
/*	
 * 1.整体
 * 2.url
 * 3.image
 * 4.title
 * 5.price
 * */
/*	echo "[taoke]".$i_table."[/taoke]";
	$pattern = '/<table.*?<\/table>/s';
	preg_match($pattern, $i_table, $matches, PREG_OFFSET_CAPTURE, 0);
	var_dump($matches);
*/	
	if ( preg_match($pattern, $i_table, $matches, PREG_OFFSET_CAPTURE, 0) ) {
			$taoke_info = array(
				'taoke_table' => $matches[0][0],
				'taoke_url' => $matches[1][0],
				'taoke_image' => $matches[2][0],
				'taoke_title' => $matches[3][0],
				'taoke_price' => $matches[4][0]			
				);
	}else {
		exit('<div class="box error">Error[1]:淘客推广代码出错！</div>');
	}
	if (empty($taoke_info['taoke_url'])){
		exit('<div class="box error">Error[2]淘客推广代码出错！</div>');
	}
	$sqlSelect = "SELECT id, url FROM taoke WHERE url = '".$taoke_info['taoke_url']."'";
	$result = $dbh->query($sqlSelect);
	$row = $result->fetch(PDO::FETCH_BOTH);
	if (!empty($row[0])){ //已含有URL
		?>
<div class="box old">已含有推广链接:<?php echo $taoke_info['taoke_url']; ?></div>	
		<?php 
	}else {
		$sqlInsert = "INSERT INTO taoke (id, i_url, i_title, i_table, image, price, title, url) 
			VALUES(null, '".$i_url."', '".$i_title."',
			'".$taoke_info['taoke_table']."', '".$taoke_info['taoke_image']."',
			'".$taoke_info['taoke_price']."', '".$taoke_info['taoke_title']."',
			'".$taoke_info['taoke_url']."' )";
		if ($dbh->exec($sqlInsert) != 1){
			?>
<div class="box">URL submit <span class="error">Error</span>!<br />URL: <span class="error"><?php echo $taoke_info['taoke_url']; ?></span></div>
			<?php 
			}else {
			?>
<div class="box">URL submit <span class="success">Successfully</span>!<br />URL: <span class="success"><?php echo $taoke_info['taoke_url']; ?></span></div>
			<?php
			}
	} //END 插入信息
	
} //END if Submit
?>

<form action="submit.php" method="post" onsubmit="return sendtb(this);">
	<br /><b>淘小七的标题</b>: <input name="title" size="80" />
	<br />&nbsp;<br /><b>淘小七的链接</b>: <input name="url" size="80" />
	<br />&nbsp;<br /><b>淘客推广TABLE</b>:
	<br /><textarea name="table" cols="70" rows="10"></textarea>
	<div style="margin-left:100px;">
		<input name="submit" type="submit" value="提交数据库" />
		<input name="submit" type="reset" value="重置" />
	</div>
<script language="Javascript" type="text/javascript">
<!--
  function sendtb(theForm)
  {
    if (document.layers == null)
    {
      var error = "";
      error = (theForm.url.value == "") ? "淘小七的链接必须输入！\n" : "";
      error = (theForm.title.value == "") ? "淘小七的标题必须输入！\n" : "";
      error = (theForm.table.value == "") ? "淘客推广代码肯定要输入的啦，傻B\n" : "";
      if (error != "")
      {
        alert(error);
        return false;
      }
    //  theForm.action = theForm.url.value;
      return true;
    }
  }
// -->
</script>
</form>

</div><!-- #wrapper -->
</body>
</html>